﻿using K9Nano.Dependency;

namespace K9Nano.Settings;

internal class SettingDefinitionManager : ISettingDefinitionManager, ISingletonDependency
{
    private readonly IDictionary<string, SettingDefinition> _settings;

    public SettingDefinitionManager()
    {
        _settings = new Dictionary<string, SettingDefinition>();
    }

    public void Create(IEnumerable<SettingDefinition> settings)
    {
        foreach (var setting in settings)
        {
            if (_settings.ContainsKey(setting.Name))
            {
                throw new Exception("There is already a setting defined with name: " + setting.Name);
            }
            _settings.Add(setting.Name, setting);
        }
    }

    public SettingDefinition GetSettingDefinition(string name)
    {
        if (!_settings.TryGetValue(name, out var settingDefinition))
        {
            throw new KeyNotFoundException("There is no setting defined with name: " + name);
        }

        return settingDefinition;
    }

    public bool TryGetSettingDefinition(string name, out SettingDefinition? definition)
    {
        var result = _settings.TryGetValue(name, out var settingDefinition);
        definition = settingDefinition;
        return result;
    }

    public IReadOnlyList<SettingDefinition> GetAllSettingDefinitions()
    {
        return _settings.Values.ToList();
    }
}